﻿using AutoMapper;
using BanquetPortion.Dtos.BanquetGiftPeopleDtos;
using BanquetPortion.Models;
using BanquetPortion.Services;
using Microsoft.AspNetCore.Mvc;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BanquetPortion.Controllers
{
    [Route("api/banquetGiftPeoples")]
    [ApiController]
    public class BanquetGiftPeoplesController : ControllerBase
    {
        private readonly IBanquetGiftPeopleRepostory _banquetGiftPeopleRepostory;
        private readonly IMapper _mapper;
        public BanquetGiftPeoplesController(IBanquetGiftPeopleRepostory banquetGiftPeopleRepostory, IMapper mapper)
        {
            _banquetGiftPeopleRepostory = banquetGiftPeopleRepostory;
            _mapper = mapper;
        }
        [HttpGet]
        public async Task<IActionResult> GetBanquetGiftPeopleAsync()
        {
            var banquetGiftPeopleFromRepo = await _banquetGiftPeopleRepostory.GetBanquetGiftPeopleAsync();
            if(banquetGiftPeopleFromRepo == null)
            {
                return NotFound("还未存入数据!");
            }
            var banquetGiftPeopleFromDto = _mapper.Map<IEnumerable<BanquetGiftPeopleDto>>(banquetGiftPeopleFromRepo);
            return Ok(banquetGiftPeopleFromDto);
        }
        [HttpGet("{banquetGiftPeopleId}", Name =nameof(GetBanquetGiftPeopleAsync))]
        public async Task<IActionResult> GetBanquetGiftPeopleAsync([FromRoute] int banquetGiftPeopleId)
        {
            var banquetGiftPeopleFromRepo = await _banquetGiftPeopleRepostory.GetBanquetGiftPeopleByIdAsync(banquetGiftPeopleId);
            if(banquetGiftPeopleFromRepo == null)
            {
                return NotFound("该资源不存在");
            }
            var banquetGiftPeopleFromDto = _mapper.Map<BanquetGiftPeopleDto>(banquetGiftPeopleFromRepo);
            return Ok(banquetGiftPeopleFromDto);
        }
        [HttpPut("{banquetGiftPeopleId}")]
        public async Task<IActionResult> UpdataBanquetGiftPeopleAsync([FromRoute] int banquetGiftPeopleId, [FromBody] BanquetGiftPeopleForUpdateDto banquetGiftPeopleForUpdateDto)
        {
            if (!await _banquetGiftPeopleRepostory.BanquetGiftPeopleExistsAsync(banquetGiftPeopleId))
            {
                return NotFound("该资源不存在");
            }
            var banquetGiftPeopleFromRepo = await _banquetGiftPeopleRepostory.GetBanquetGiftPeopleByIdAsync(banquetGiftPeopleId);
            _mapper.Map(banquetGiftPeopleForUpdateDto, banquetGiftPeopleFromRepo);
            await _banquetGiftPeopleRepostory.SaveAsync();
            var banquetGiftPeople = await _banquetGiftPeopleRepostory.GetBanquetGiftPeopleByIdAsync(banquetGiftPeopleId);
            var banquetGiftPeopleFromDto = _mapper.Map<BanquetGiftPeople>(banquetGiftPeople);
            return Ok(banquetGiftPeopleFromDto);
        }
        [HttpPost]
        public async Task<IActionResult> CreateBanquetGiftPeopleAsync([FromBody] BanquetGiftPeopleForCreateDto banquetGiftPeopleForCreateDto)
        {
            var banquetGiftPeople = _mapper.Map<BanquetGiftPeople>(banquetGiftPeopleForCreateDto);
            _banquetGiftPeopleRepostory.CreateBanquetGiftPeople(banquetGiftPeople);
            await _banquetGiftPeopleRepostory.SaveAsync();
            var banquetGiftPeopleFromDto = _mapper.Map<BanquetGiftPeopleDto>(banquetGiftPeople);
            return CreatedAtRoute(nameof(GetBanquetGiftPeopleAsync), new { banquetGiftPeopleId = banquetGiftPeopleFromDto.GiftPeopleName }, banquetGiftPeopleFromDto);
        }
        [HttpDelete("{banquetGiftPeopleId}")]
        public async Task<IActionResult> DeleteBanquetGiftPeopleAsync([FromRoute] int banquetGiftPeopleId)
        {
            var banquetGiftPeopleFromRepo = await _banquetGiftPeopleRepostory.GetBanquetGiftPeopleByIdAsync(banquetGiftPeopleId);
            if (banquetGiftPeopleFromRepo == null)
            {
                return NotFound("该资源不存在");
            }
            _banquetGiftPeopleRepostory.DeleteBanquetGiftPeople(banquetGiftPeopleFromRepo);
            await _banquetGiftPeopleRepostory.SaveAsync();
            return NoContent();
        }
    }
}
